//index.js
//获取应用实例
const app = getApp()
import {
    createStoreBindings 
} from '../../miniprogram_npm/mobx-miniprogram-bindings/index'
import {store} from '../../store/store'
Page({
    data: {
        navHeight: app.globalData.statusBarHeight * 2 + 86,
        messageList: [],
        nowUser: app.globalData.userId,
        navbarData: {
            title: '消息'
        },
        nowUserId: app.globalData.userId,
        pageIndex: 1,
        otherMessages: null,
        unLogin: false,
        hasOnShow:false
    },
    getMessage: function () {
        var that = this;
        wx.request({
            url: app.getUrl('/xcx/pt/userNoReadMessage'),
            method: 'GET',
            header: wx.getStorageSync('cookie'),
            success(res) {
                let messageList = res.data.data;
                if (messageList) {
                        wx.showTabBarRedDot({
                            index: 1,
                            success(res) {
                                console.log(res)
                            }
                        })
                } else {
                    wx.hideTabBarRedDot({
                        index: 1,
                        success(res) {
                            console.log(res)
                        }
                    })
                    
                }
            }
        })
    },
    onLoad:function(){
        this.storeBindings = createStoreBindings(this, {
            store,
            fields: ["numA", "numB", "sum"],
            actions:{
                getUnRead:'getUnRead'
            },
          });
    },
    onUnload() {
        this.storeBindings.destroyStoreBindings();
      },
    onShow: function () {
        // this.getTabBar().show()
        if (this.data.hasOnShow) {
            return
          }
          this.setData({
            hasOnShow:true
          })
        this.getUnRead()
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: app.globalData.tabIndex
            })
        }
        this.setData({
            messageList: null,
            pageIndex:1
        })
        wx.showLoading({
            title: "加载中",
            mask: true
        })
        var that = this;
        wx.request({
            url: app.getUrl('/xcx/pt/messageList'),
            method: 'GET',
            header: wx.getStorageSync('cookie'),
            data: {
                "pageIndex": that.data.pageIndex
            },
            success(res) {
                if (res.data.code == 0) {
                    that.setData({
                        messageList: res.data.data.data,
                        nowUser: app.globalData.userId,
                        pageIndex: res.data.data.pageIndex,
                        pageCount: res.data.data.pageCount
                    })
                } else if (res.data.code == 401) {
                    that.setData({
                        unLogin: true
                    })
                }
                wx.hideLoading();
            }
        })
        // wx.request({
        //     url: app.getUrl('/xcx/pt/otherMessages'),
        //     method: 'GET',
        //     header: wx.getStorageSync('cookie'),
        //     success(res) {
        //         if (res.data.code == 0) {
        //             that.setData({
        //                 otherMessages: res.data.data,
        //             })
        //         }
        //         wx.hideLoading();
        //     }
        // })
     
    },
    onHide(){
        this.setData({
            hasOnShow:false
          })
    },
    // 前往消息
    toMessage: function (e) {
        var that = this;
        
        wx.request({
            url: app.getUrl('/xcx/pt/messageRead'),
            method: 'GET',
            header: wx.getStorageSync('cookie'),
            data: {
                messageId: e.currentTarget.dataset.id
            },
            success(res) {
                that.setData({
                    messageList: res.data.data
                })
                // that.getMessage()
            }
        })
        let {
            category,
            url,
            id
        } = e.currentTarget.dataset
        console.log('888url',url)
        if (category == 1 || category==4) {
            console.log('url',url + "?id=" + id)
            wx.navigateTo({
                url: url + "?id=" + id,
            })
        }
        else if(category == 7){
            wx.navigateTo({
                url: '/pages/childFollowUp/onlineFollowUpDetail/onlineFollowUpDetail?id='+ id,
            })
        } else if(category == 2){
            let linkUrl = url.replace('/pages/report/reportTypeList/reportTypeList','/pages/report/webview/webview')
            console.log('linkUrl',linkUrl)
            wx.navigateTo({
                url: linkUrl+"&proxy=true",
            })
        }else if(category == 9){
            let linkUrl = url
            wx.navigateTo({
                url: linkUrl+"&proxy=true",
            })
        }else if(category == 5){
          let aaa = url.split('?')[0] + "?url=" + url.split('?')[1] + "&category=5"
          console.log(aaa);
            wx.navigateTo({
                url: aaa,
            })
        }else{
          let aaa = url.split('?')[0] + "?url=" + url.split('?')[1]
            wx.navigateTo({
                url: aaa,
            })
        }
    },
    loadMoreData: function () {
        var that = this;
        var currentPage = that.data.pageIndex; // 获取当前页码
        if (that.data.pageCount > currentPage) {
            currentPage += 1; // 加载当前页面的下一页数据
            var tips = "加载中";
            wx.showLoading({
                title: tips,
            })

            // 请封装自己的网络请求接口，这里作为示例就直接使用了wx.request.
            wx.request({
                url: app.getUrl('/xcx/pt/messageList'),
                method: 'GET',
                header: wx.getStorageSync('cookie'),
                data: {
                    "pageIndex": currentPage
                },
                success(res) {
                    var newMessages = res.data.data.data;
                    // var originDatas = that.data.messageList;
                    // var newDatas = [];
                    that.data.messageList.push(...newMessages);
                    
                    // if(res.data.data.pageIndex<=res.data.data.pageCount){
                    //     this.messageList.push(...newMessages);
                    // }
                   
                    // for (var i = 0; i < newMessages.length; i++) {
                    //     if (originDatas.indexOf(newMessages[i]) == -1) {
                    //         this.messageList.push(newMessages[i]);
                    //     }
                    // }
                    wx.hideLoading();
                    that.setData({
                        messageList:  that.data.messageList,
                        nowUser: app.globalData.userId,
                        pageIndex: res.data.data.pageIndex,
                    })
                }
            })
        }else{
            wx.hideLoading();
        }
       
    }
})